package com.jorado.apollo.service;

import com.ctrip.framework.apollo.core.ConfigConsts;
import com.google.common.base.Strings;
import com.jorado.apollo.model.NamespaceReleaseModel;
import org.apache.http.impl.client.CloseableHttpClient;

public class ReleaseApiService extends AbstractApiService {

    public ReleaseApiService(CloseableHttpClient client, String baseUrl) {
        super(client, baseUrl);
    }

    public void release(String appId, String env, String clusterName, String namespaceName, NamespaceReleaseModel releaseModel) {

        if (Strings.isNullOrEmpty(clusterName)) {
            clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT;
        }

        checkNotEmpty(appId, "App id");
        checkNotEmpty(env, "Env");

        String path = String.format("apps/%s/envs/%s/clusters/%s/namespaces/%s/releases", escapePath(appId), escapePath(env), escapePath(clusterName), escapePath(namespaceName));
        try {

            post(path, releaseModel);

        } catch (Throwable ex) {
            throw new RuntimeException(String
                    .format("release namespace for appId: %s, cluster: %s, namespace: %s in env: %s failed", appId, clusterName,
                            namespaceName, env), ex);
        }
    }
}
